package com.financial;

/**
 * @Author:zxh
 * @Description: 复利计算
 * @Date: 2021/3/20
 * @Modified By:
 */
public class CompoundInterestMain {
    //要素： 利率   每年的投入的金额   总金额
    //结果： 输出年数

    /**
     *
     * @param interestRate 年利率
     * @param yearAmount 每年投入的钱
     * @param totalAmount 希望获取的钱总额
     * @return
     */
    public static int count(Double interestRate,int yearAmount,int totalAmount){
        int amount = 0;
        int year = 0;
       while (true){
           amount += yearAmount;
           amount += amount * interestRate;
           year ++;
           if (amount >= totalAmount) {
               System.out.println("已存储:"+amount+"元");
               break;
           }
       }
      return year;
    }

    /**
     *
     * @param monthSpend 每月花费的钱
     * @param yearMany 希望每年存的钱
     * @return
     */
    public static int yearMany(int monthSpend,int yearMany){
        return yearMany / 12 +2000+ monthSpend;
    }


    /**
     *
     * @param interestRate 年利率
     * @param yearMany 每年投入的钱
     * @param yearNum 持续投入的年数
     * @return
     */
    public static int manyCount(Double interestRate,int yearMany,int yearNum){
        int amount = 0;
        for (int i = 0; i < yearNum ; i++) {
            amount += yearMany;
            amount += amount * interestRate;
        }
        return amount;
    }

    public static void main(String[] args) {
        int count = CompoundInterestMain.count(0.1, 30, 1000);
        System.out.println("输出："+count+"年");
        int i = CompoundInterestMain.yearMany(3500, 250000);
        System.out.println("月："+ i+"元");
        int manyCount = CompoundInterestMain.manyCount(0.1, 30, 11);
        System.out.println("一共"+manyCount+"元");
    }


}
